# -*- coding: utf-8 -*-


import os

#builtins import
import wx
import wx.lib.filebrowsebutton as filebrowse

#core util import
import pystore.core.i18n as i18n
import pystore.core.framework.logger as logger

#ui import
import facade2
import image_util 
import framework
import dialogs 
import memento_key
import validators
_t=i18n.t_
_log=logger.getLog('ui') 

class AddDiskRepositoryDialog(framework.BaseDialog):
    def __init__(self, parent):
        super(AddDiskRepositoryDialog,self)
        framework.BaseDialog.__init__(self,parent,_t('Add Disk Repository'))
        
    def createContentPane(self,parent):
        '''
        override parent
        '''
        result=wx.Panel(parent)
        startDirectory=''
        self.dirBrowse = filebrowse.DirBrowseButton(result,
                                                    startDirectory=startDirectory,
                                                    labelText=_t('Store At:'),
                                                    newDirectory=True,
                                                    )
        self.dirBrowse.SetValidator( validators.PathValidator(self))
        self.dirBrowse.SetValue(startDirectory)
        sizer = wx.BoxSizer(wx.VERTICAL)
        
        sizer.Add((10,10),flag=wx.EXPAND)
        sizer.Add(self.dirBrowse,flag=wx.EXPAND)

        #惯例方法
        result.SetSizer(sizer)
        sizer.Fit(result)
        sizer.SetSizeHints(result)
        return result
    
    def storeLocation(self):
        return self.dirBrowse.GetValue()

    def addDiskRepository(self):
        facade2.manager.addDiskRepository(self.storeLocation())


#todo 检查有效性:目录无效
class ProjectPathDialog(wx.Dialog):
    def __init__(self, parent,name="",dir="",title=""):
        wx.Dialog.__init__(self,parent,title=title)
        
        # Now continue with the normal construction of the dialog
        # contents
        sizer = wx.BoxSizer(wx.VERTICAL)

        sizer.Add(wx.StaticText(self, -1, title), 0, wx.ALIGN_CENTRE|wx.ALL, 5)

        box = wx.BoxSizer(wx.HORIZONTAL)
        box.Add(wx.StaticText(self, -1, _t('Name:')), 0, wx.ALIGN_CENTRE|wx.ALL, 5)

        self.textName = wx.TextCtrl(self, -1, name, size=(80,-1))
        box.Add(self.textName, 1, wx.ALIGN_CENTRE|wx.ALL, 5)

        sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        box = wx.BoxSizer(wx.HORIZONTAL)

        self.dirBrowse = filebrowse.DirBrowseButton(self,startDirectory=dir,size=(450, -1),labelText=unicode("目录:",'utf-8'))
        self.dirBrowse.textControl.SetValue(dir)

        box.Add(self.dirBrowse, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
        sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
        sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5)

        btnsizer = wx.StdDialogButtonSizer()
        
        btn = wx.Button(self, wx.ID_OK)
        btn.SetDefault()
        btnsizer.AddButton(btn)

        btn = wx.Button(self, wx.ID_CANCEL)
        btnsizer.AddButton(btn)
        btnsizer.Realize()

        sizer.Add(btnsizer, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        self.SetSizer(sizer)
        sizer.Fit(self)
        
    def getDir(self):
        return self.dirBrowse.GetValue()
    
    def getName(self):
        return self.textName.GetValue()
    
    def setNameEnable(self,isEnable):
        #若用text.Enable将不能选择和粘贴文本,有时候想复制一下都不行
        self.textName.SetEditable(isEnable)
        self.textName.SetBackgroundColour('#dddddd') #灰色
        
        